package com.wocao.controller.uniapp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wocao.entity.*;
import com.wocao.service.*;
import org.apache.commons.math3.analysis.function.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/uni-app")
public class CommentsAppController {

    //将zyOwner表从容器中获取
    @Autowired
    private ZyOwnerRoomService zyOwnerRoomService;

    @Autowired
    private CommentsAppService commentsAppService;

    @Autowired
    private ZyCommunityInteractionService interactionService;

    @Autowired
    private ZyCommentService zyCommentService;

    @Autowired
    private ZyLikeService zyLikeService;

    /*显示*/
    @PostMapping("zyCommunityInteraction")
    public ResponseResult<List<ZyCommunityInteraction>> zyCommunityInteraction() {
        try {
//            LambdaQueryWrapper<ZyCommunityInteraction> wrapper = new LambdaQueryWrapper<>();
//            wrapper.orderByDesc(ZyCommunityInteraction::getCreateBy);
            List<ZyCommunityInteraction> list = commentsAppService.selectAll();
            /*使用xml完成多条件查询*/
            return ResponseResult.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(500L, "查询失败" + e.getMessage());
        }
    }


    //ZyCommunityInteraction表、ZyComment表、ZyOwner表三表联查
    @GetMapping("/{interactionId}")
    public ResponseResult<List<ZyCommunityInteraction>> getCommunityInteractionMap(@PathVariable String interactionId) {
        List<ZyCommunityInteraction> communityInteractionMap =
                interactionService.getCommunityInteractionMap(new ZyCommunityInteraction(interactionId));
        return ResponseResult.ok(communityInteractionMap);
    }

    // 更具commentId查询对应的zyOwner昵称
    @GetMapping("/zyOwner/{commentId}")
    public ResponseResult<String> getOwnerNickName(@PathVariable String commentId) {
        String ownerNickName = commentsAppService.getOwnerNickName(commentId);
        return ResponseResult.ok(ownerNickName);
    }

    // 添加一条ZyCommunityInteraction评论用PutMapping
    @PutMapping("/zyCommunityInteraction")
    public ResponseResult<String> addZyCommunityInteraction(@RequestBody ZyCommunityInteraction zyCommunityInteraction) {
        try {

              System.out.println("zyCommunityInteraction：" + zyCommunityInteraction);
            zyCommunityInteraction.setCreateTime(LocalDateTime.now());
            zyCommunityInteraction.setUserId(zyCommunityInteraction.getCreateBy());
            /*在zyOwnerService中根据zyCommunityInteraction.ownerId查询小区id Community*/
            zyCommunityInteraction.setCommunityId(zyOwnerRoomService.getByuserId(zyCommunityInteraction.getUserId()));
            interactionService.save(zyCommunityInteraction);
            return ResponseResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(500L, "添加失败" + e.getMessage());
        }
    }

    // 删除一条ZyCommunityInteraction评论
    @DeleteMapping("/zyCommunityInteraction/{interactionId}")
    public ResponseResult<String> deleteZyCommunityInteraction(@PathVariable String interactionId) {
        try {
            interactionService.removeById(interactionId);
            return ResponseResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(500L, "删除失败" + e.getMessage());
        }
    }

    // 发送一条ZyComment评论
    @PutMapping("/zyComment")
    public ResponseResult<String> addZyComment(@RequestBody ZyComment zyComment) {
        try {
            zyComment.setCreateTime(LocalDateTime.now());
            zyCommentService.save(zyComment);
            return ResponseResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(500L, "添加失败" + e.getMessage());
        }
    }

    // 添加一个点赞数
    @PutMapping("/like")
    public ResponseResult<String> addLike(@RequestBody String zyLike) {
        try {
            ZyLike zyLike1 = new ZyLike();
            zyLike1.setInteractionId(zyLike);
            zyLikeService.save(zyLike1);
            ZyCommunityInteraction interaction = interactionService.getById(zyLike);
            if (interaction.getLikeCount() == null){
                interaction.setLikeCount(0);
            }
            interaction.setLikeCount(interaction.getLikeCount() + 1); // 减少点赞数
            interactionService.updateById(interaction);
            return ResponseResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(500L, "点赞失败" + e.getMessage());
        }
    }


    // 删除一个点赞数
    @DeleteMapping("/like/{zyLike}")
    public ResponseResult<String> deleteLike(@PathVariable String zyLike) {
        try {
            List<ZyLike> list = zyLikeService.list();
            for (ZyLike zyLike1 : list) {
                if (zyLike1.getInteractionId().equals(zyLike)) {
                    zyLikeService.removeById(zyLike1.getLikeId());
                }
            }
            ZyCommunityInteraction interaction = interactionService.getById(zyLike);
                if (interaction.getLikeCount() == null || interaction.getLikeCount() == 0){
                    interaction.setLikeCount(0);
                }else {
                    interaction.setLikeCount(interaction.getLikeCount() - 1); // 减少点赞数
                    interactionService.updateById(interaction);
                }
            return ResponseResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(500L, "取消失败" + e.getMessage());
        }
    }

    // 删除一个点赞数
    @PostMapping("/like")
    public ResponseResult getLike() {
        try {
            List<ZyLike> list = zyLikeService.list();
            return ResponseResult.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(500L, "查询失败" + e.getMessage());
        }
    }

}
